<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      Function.prototype.myCall = function (ct) {
        // console.log(ct, this);
        //  谁调用call，此时的this就指向谁,此时的this是test函数，ct为参数
        var curCt = ct || window;
        curCt.fn = this;
        // 拿到后续参数
        const args = [...arguments].slice(1);
        // 相当于就是执行test且包含后续参数
        const res = curCt.fn(...args);
        // 添加临时属性fn后需要删除
        delete fn;
        // 如果有返回值要返回
        return res;
      };

      var foo = {
        value: "fooValue",
      };

      const test = function (name, age, sex) {
        return {
          value: this.value,
          name,
          age,
          sex,
        };
      };
      console.log(test.myCall(foo, "ssss", 18, "M"));
    </script>
  </body>
</html>
